﻿package src.computation 
{
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	import flash.utils.Dictionary;
	// src
	import src.entity.Node;
	
	public class CNode extends CEntity implements ICEntity, ICObjectOfScheme {
		
		public var links:Dictionary;	// ids of connected links [id]=id
		public const type = Node.TYPE;
		
		public function CNode(){			
		}
		
		public function addLink(link:CLink, allLink):Boolean {			
			//this link is already connected to this node
			if (link.end != null && link.end["id"] == this.id && link.end["type"] == this.type) { 
				return false;
			}			
			//this link is already connected to this node
			if (link.start != null && link.start["id"] == this.id && link.start["type"] == this.type) { 
				return false;				
			}			
			var idLink;
			for each(idLink in this.links) {
				var l = allLink[idLink];
				if (l != null) {
					if (link.end != null) {
						if (l.end != null && link.end["id"] == l.end["id"] && link.end["type"] == l.end["type"]) {
							return false;
						}
						if (l.start != null && link.end["id"] == l.start["id"] && link.end["type"] == l.start["type"]) {
							return false;
						}
					}					
					if (link.start != null) {
						if (l.end != null && link.start["id"] == l.end["id"] && link.start["type"] == l.end["type"]) {
							return false;
						}
						if (l.start != null && link.start["id"] == l.start["id"] && link.start["type"] == l.start["type"]) {
							return false;
						}						
					}
				}
			}
			if (this.links == null) {
				this.links = new Dictionary();
			}			
			links[link.id] = link.id;
			link.addEnd(this);
			return true;
		}
		
		/*
		* 	unLink
		* 	@id - id of link
		*/ 
		public function unLink(id:Number):void { 			
			if (links[id] != null) {				
				delete links[id];
			}			
		}
		
		/**
		 * haveSuchLink
		 * @param	id
		 * @return
		 */
		public function haveSuchLink(id):Boolean {
			if (links[id] != null) {
				return true;
			}
			return false;
		}
		
		public function print():void {
			var l;
			var str = "";
			for each(l in links) {
				str = str + l + ";";
			}
			trace("------" + this.type + "----------");
			this.printBase();	
			trace("links:" + str);
			trace("---------------------------------");
		}
		
		/**
		 * clone
		 * return copy of this object
		 */
		public override function clone() {			
			var cloneObj = new CNode();
			// common parametrs
			cloneObj.id = this.id;
			cloneObj.name = this.name;
			cloneObj.description = this.description;
			//
			var dict = new Dictionary();
			for (var key in this.links) {
				dict[key] = this.links[key];
			}
			cloneObj.links = dict;
			return cloneObj;
		}
		
	}
	
}